package dataAccess;

import java.sql.SQLException;
import java.util.ArrayList;

import org.apache.log4j.Logger;

import epam.car.rent.*;

/**
 * A class that allows you to manage work with database
 * @author Alexander Tretyak
 *
 */
public final class DbWriter {
	static Logger log = Logger.getLogger("carrentlogger");
	private static final CarRentDaoImpl crDao = new CarRentDaoImpl();
	@Deprecated
	public static void requestAllFromDb() {
		try {
			crDao.start();
			Management.importClientsFromList((ArrayList<Client>) crDao.requestClients());
			Management.importCarsFromList((ArrayList<Car>) crDao.requestCars());
			Management.importManagersFromList((ArrayList<Manager>) crDao.requestManagers());
			Management.importOrdersFromList((ArrayList<Order>) crDao.requestOrders());
		} catch (SQLException e) {
			System.out.println(e.getMessage());
			e.printStackTrace();
		}finally{
			try {
				crDao.stop();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
	
	public static void storeAllToDb() {
		try {
			crDao.start();
			crDao.storeClients();
			crDao.storeCars();
			crDao.storeManagers();
			crDao.storeOrders();
			crDao.stop();
			System.out.println("All objects are stored!");
		} catch (SQLException e) {
			System.out.println(e.getMessage());
			e.printStackTrace();
		}
	}
	
	public static long addToDb(Management item) {
		long id=-1;
		try {
			crDao.start();
			id = item.getId();
			switch (Entities.valueOf(item.getClass().toString().replaceAll("^.+\\W", "").toUpperCase())) {
			case CLIENT:
				crDao.createClient(item);
				break;
			case CAR:
				crDao.createCar(item);
				break;
			case MANAGER:
				crDao.createManager(item);
				break;
			case ORDER:
				crDao.createOrder(item);
				break;
			}
			crDao.stop();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return id;
	}
	public static void update(Management item) {
		try {
			crDao.start();
			switch (Entities.valueOf(item.getClass().toString().replaceAll("^.+\\W", "").toUpperCase())) {
			case CLIENT:
				crDao.updateClient((Client)item);
				break;
			case CAR:
				crDao.updateCar((Car)item);
				break;
			case ORDER:
				crDao.updateOrder((Order)item);
				break;
			case MANAGER:
				crDao.updateManager((Manager)item);
				break;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try {
				crDao.stop();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	public static void delete(Management item) {
		try {
			crDao.start();
			switch (Entities.valueOf(item.getClass().toString().replaceAll("^.+\\W", "").toUpperCase())) {
			case CLIENT:
				crDao.deleteClient((Client)item);
				break;
			case CAR:
				crDao.deleteCar((Car)item);
				break;
			case ORDER:
				crDao.deleteOrder((Order)item);
				break;
			case MANAGER:
				crDao.deleteManager((Manager)item);
				break;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try {
				crDao.stop();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	public static Client getClientById(String id) {
		Client client = null;
		try {
			crDao.start();
			client = crDao.getClientById(id);

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try {
				crDao.stop();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return client;
	}	
	
	
	
	public static Manager getManagerById(String id) {
		Manager manager = null;

		try {
			crDao.start();
			manager = crDao.getManagerById(id);

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try {
				crDao.stop();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return manager;
	}	
	
	public static Car getCarById(Long id) {
		Car car = null;
		try {
			crDao.start();
			car = crDao.getCarById(id);

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try {
				crDao.stop();
				return car;
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return null;
	}
	
	public static Order getOrderById(Long id) {
		Order o = null;
		try {
			crDao.start();
			o = crDao.getOrderById(id);

		} catch (SQLException e) {
			log.warn("Problem in getting order by id: "+e.getMessage());
		}finally{
			try {
				crDao.stop();
				return o;
			} catch (SQLException e) {
				log.debug(e.getMessage());
			}
		}
		return null;
	}
	
	public static ArrayList<Car> getAllCars() {
		ArrayList<Car> cars = new ArrayList<Car>();
		try {
			crDao.start();
			
			cars = crDao.requestCars();
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try {
				crDao.stop();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return cars;
	}
	
	public static ArrayList<Order> getOrdersForClient(String id) {
		ArrayList<Order> orders = new ArrayList<Order>();
		try {
			crDao.start();
			
			orders = crDao.getOrdersForClient(id);
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try {
				crDao.stop();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return orders;
	}
	
	public static ArrayList<Order> getAllOrders() {
		ArrayList<Order> orders = new ArrayList<Order>();
		try {
			crDao.start();
			
			orders = crDao.requestOrders();
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try {
				crDao.stop();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return orders;
	}
	
	public static ArrayList<Car> getFreeCars() {
		ArrayList<Car> cars = new ArrayList<Car>();
		try {
			crDao.start();
			
			cars = crDao.requestFreeCars();
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try {
				crDao.stop();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return cars;
	}	
	
	public static ArrayList<Client> getAllClients() {
		ArrayList<Client> clients = new ArrayList<Client>();
		try {
			crDao.start();
			clients = crDao.requestClients();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try {
				crDao.stop();
				return clients;
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return null;
	}	
}
